import type { FiveElement } from '../../constants/five-elements';
import type { CultivationSkillItemId } from '../../data/item/cultivation-skill';

export const CONDITION_TYPE = {
  /** 境界 */
  REALM: 'REALM',
  /** 功法学习数量 */
  CULTIVATION_SKILL_STUDY_COUNT: 'CULTIVATION_SKILL_STUDY_COUNT',
  /** 灵根 */
  SPIRITUAL_ROOT: 'SPIRITUAL_ROOT',
  /** 击杀妖兽 */
  KILL_MONSTER: 'KILL_MONSTER',
  /** 物品 */
  ITEM: 'ITEM',
  /** 宗门职位 */
  SECT_JOB: 'SECT_JOB',
  /** 战斗等级 */
  BATTLE_LEVEL: 'BATTLE_LEVEL',
  /** 灵石数量 */
  SPIRIT_STONE: 'SPIRIT_STONE',
  /** 寿元 */
  LIFE: 'LIFE',
  /** 功法条件 */
  CULTIVATION_SKILL: 'CULTIVATION_SKILL',
  AND: 'AND',
  OR: 'OR',
  NOT: 'NOT',
} as const;

export type ConditionType =
  (typeof CONDITION_TYPE)[keyof typeof CONDITION_TYPE];

// 条件类型定义
export interface BaseCondition {
  type: string;
  description?: string;
}

/** 境界条件 */
export interface RealmCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.REALM;
  realm: number;
}

/** 功法学习数量条件 */
export interface CultivationSkillStudyCountCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.CULTIVATION_SKILL_STUDY_COUNT;
  count: number;
}

/** 灵根条件 */
export interface SpiritualRootCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.SPIRITUAL_ROOT;
  spiritualRoot: FiveElement[];
}

/** 击杀怪物条件 */
export interface KillMonsterCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.KILL_MONSTER;
  monsterId: string;
  count: number;
}

// 物品条件
export interface ItemCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.ITEM;
  itemId: string;
  count: number;
}

// 宗门职位条件
export interface SectJobCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.SECT_JOB;
  job: number;
}

// 战斗等级条件
export interface BattleLevelCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.BATTLE_LEVEL;
  level: number;
}

// 灵石数量条件
export interface SpiritStoneCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.SPIRIT_STONE;
  count: number;
}

// 寿命条件
export interface LifeCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.LIFE;
  years: number;
}

// 功法条件
export interface CultivationSkillCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.CULTIVATION_SKILL;
  id: CultivationSkillItemId;
}

// 复合条件
export interface AndCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.AND;
  conditions: Condition[];
}

export interface OrCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.OR;
  conditions: Condition[];
}

export interface NotCondition extends BaseCondition {
  type: typeof CONDITION_TYPE.NOT;
  condition: Condition;
}

// 联合类型
export type Condition =
  | RealmCondition
  | CultivationSkillStudyCountCondition
  | SpiritualRootCondition
  | KillMonsterCondition
  | ItemCondition
  | SectJobCondition
  | BattleLevelCondition
  | SpiritStoneCondition
  | LifeCondition
  | CultivationSkillCondition
  | AndCondition
  | OrCondition
  | NotCondition;

export type ConditionProgressType = ConditionType | 'UNKNOWN';

// 进度信息接口
export interface ConditionProgress {
  isMet: boolean; // 是否满足条件
  current: number; // 当前值
  required: number; // 需求值
  description: string; // 条件描述
  progress?: number; // 进度百分比 0-1
  type: ConditionProgressType; // 条件类型
  subConditions?: ConditionProgress[]; // 用于 AND/OR 条件
  subCondition?: ConditionProgress; // 用于 NOT 条件
}
